package org.csu.lestore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.lestore.api.common.CommonResponse;
import org.csu.lestore.api.entity.*;
import org.csu.lestore.api.persistence.CartMapper;
import org.csu.lestore.api.persistence.ImgMapper;
import org.csu.lestore.api.persistence.ItemMapper;
import org.csu.lestore.api.persistence.ProductMapper;
import org.csu.lestore.api.service.CartService;
import org.csu.lestore.api.util.ImgUtil;
import org.csu.lestore.api.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("cartService")
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ImgMapper imgMapper;

    @Override
    public CommonResponse<Cart> insertCartItem(Cart cartItem) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",cartItem.getUserId());
        queryWrapper.eq("item_id",cartItem.getItemId());
        Cart existCart = cartMapper.selectOne(queryWrapper);
        if(existCart != null){
            int count=existCart.getItemCount()+cartItem.getItemCount();
            existCart.setItemCount(count);
            cartMapper.update(existCart,queryWrapper);
            return CommonResponse.createForSuccessMessage("修改购物车条目成功");
        }else{
            cartMapper.insert(cartItem);
            return CommonResponse.createForSuccessMessage("新增购物车条目成功");
        }
    }

    @Override
    public CommonResponse<Cart> updateCartItem(Cart cartItem) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",cartItem.getUserId());
        queryWrapper.eq("item_id",cartItem.getItemId());
        Cart existCart = cartMapper.selectOne(queryWrapper);
        if(existCart != null){
            cartMapper.update(cartItem,queryWrapper);
            return CommonResponse.createForSuccessMessage("修改购物车条目成功");
        }else{
            cartMapper.insert(cartItem);
            return CommonResponse.createForSuccessMessage("新增购物车条目成功");
        }
    }

    @Override
    public CommonResponse<Cart> deleteCartItem(Cart cartItem) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",cartItem.getUserId());
        queryWrapper.eq("item_id",cartItem.getItemId());

        cartMapper.delete(queryWrapper);
        return CommonResponse.createForSuccessMessage("删除购物车条目成功");
    }

    @Override
    public CommonResponse<IPage<CartVO>> getCartListInPage(int curPage, int size, int userId) {
        IPage<Cart> cartPage = new Page<>(curPage,size);
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        cartMapper.selectPage(cartPage,queryWrapper);

        List<Cart> cartList = cartPage.getRecords();
        List<CartVO> cartVOList = new ArrayList<>();
        IPage<CartVO> cartVOPage = new Page<>(curPage,size);

        if(cartList != null) {
            for (Cart cartItem : cartList) {
                Item item = itemMapper.selectById(cartItem.getItemId());
                Product product = productMapper.selectById(item.getProductId());
                CartVO cartItemVO = cartToCartVO(cartItem,item,product);
                cartVOList.add(cartItemVO);
            }
            cartVOPage.setTotal(cartPage.getTotal());
            cartVOPage.setPages(cartPage.getPages());
            cartVOPage.setRecords(cartVOList);
            return CommonResponse.createForSuccess(cartVOPage);
        }
        return CommonResponse.createForError("获取订单列表失败");

    }

    private CartVO cartToCartVO(Cart cartItem,Item item,Product product){
        CartVO cartVO = new CartVO();

        cartVO.setUserId(cartItem.getUserId());
        cartVO.setItemId(cartItem.getItemId());
        cartVO.setItemCount(cartItem.getItemCount());

        cartVO.setProductId(item.getProductId());
        cartVO.setStock(item.getStock());
        cartVO.setPrice(item.getPrice());
        cartVO.setAttr1(item.getAttr1());
        cartVO.setAttr2(item.getAttr2());
        cartVO.setAttr3(item.getAttr3());
        cartVO.setItemImg(item.getItemImg());

        QueryWrapper<Img> imgQueryWrapper = new QueryWrapper<>();
        imgQueryWrapper.eq("img_id",item.getItemImg());
        Img img = imgMapper.selectOne(imgQueryWrapper);

        if(img!=null){
            cartVO.setImgSrc(ImgUtil.getImageBaseSrc(img.getImgSrc(),"item"));
        }else{
            cartVO.setItemImg(-1);
        }

        cartVO.setCategoryId(product.getCategoryId());
        cartVO.setProductName(product.getProductName());
        cartVO.setProductDescn(product.getProductDescn());
        cartVO.setBussnessId(product.getUserId());

        cartVO.setUnitPrice(BigDecimal.valueOf(cartItem.getItemCount()).multiply(item.getPrice()));

        return cartVO;
    }

    @Override
    public CommonResponse<Cart> clearCartItemByUserId(int userId) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);

        cartMapper.delete(queryWrapper);
        return CommonResponse.createForSuccessMessage("清空购物车条目成功");
    }
}
